﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BSPortal.Usercontrol
{
    public partial class ChangeInfo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["Username"] == null)
                Response.Redirect("trangchu.html");
            if (!IsPostBack)
            {
                LoadCity();
                LoadDistrict();
                LoadWard();

                LoadData();
            }

        }

        protected void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row => row.Name);

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        private void LoadData()
        {
            MainService mainService = Common.GetClientService();
            User user = mainService.GetUserById(HttpContext.Current.Session["Username"].ToString());
            txtUsername.Text = user.Username;
            txtName.Text = user.Name;
            txtAddress.Text = user.Address;
            txtEmail.Text = user.Email;
            txtPhone.Text = user.Phone;
            ddlCity.SelectedValue = user.City.ToString();
            ddlDistrict.SelectedValue = user.District.ToString();
            ddlWard.SelectedValue = user.Ward.ToString();
        }

        protected void btnRegistry_Click(object sender, EventArgs e)
        {
            MainService mainService = Common.GetClientService();
            var update = mainService.UpdateUser(txtUsername.Text, txtName.Text, txtAddress.Text, txtEmail.Text, txtPhone.Text, 1, int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
            
            if(txtPassword.Text != string.Empty)
                mainService.ChangePassword(txtUsername.Text, txtPassword.Text);

            if (update == (int)Common.RetCode.SUCESS)
                Response.Write("<script>alert('Cập nhật thành công!')</script>");
            else
                Response.Write("<script>alert('Cập nhật không thành công!')</script>");
        }
    }
}